//
//  QYModifyInfoNickNameViewController.m
//  QingYue
//
//  Created by Zheng on 2017/5/23.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYModifyInfoNickNameViewController.h"

@interface QYModifyInfoNickNameViewController ()

@property (nonatomic, strong) UITableViewCell *tfdCell;
@property (nonatomic, strong) UITextField *nicknameTfd;
@property (nonatomic, strong) UIButton *clickBtn;

@end

@implementation QYModifyInfoNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改昵称";
    
    [self.tableView addSubview:self.clickBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(MSSize_Width-40);
        make.left.equalTo(self.tableView).offset(20);
        make.right.equalTo(self.tableView).offset(-20);
        make.top.mas_equalTo(80);
        make.height.mas_equalTo(50);
    }];
}

#pragma mark - 传值
- (void)setNickName:(NSString *)nickName {
    _nickName = nickName;
}

#pragma mark - Button 点击事件
- (void)buttonClickAction:(UIButton *)sender {
    
    if ([_nicknameTfd.text length] == 0) {
        return;
    }
    _nickBlock(_nicknameTfd.text);
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.tfdCell && _nickName) {
        _nicknameTfd.text = _nickName;
    }
    
    return self.tfdCell;
}

#pragma mark - TableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 51;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

#pragma mark - lazy load
- (UITableViewCell *)tfdCell {
    if (!_tfdCell) {
        _tfdCell = [[UITableViewCell alloc] init];
        _tfdCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [_tfdCell.contentView addSubview:self.nicknameTfd];
        
        [_nicknameTfd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_tfdCell.contentView);
            make.left.equalTo(_tfdCell).offset(20);
            make.right.equalTo(_tfdCell).offset(-15);
        }];
    }
    return _tfdCell;
}

- (UITextField *)nicknameTfd {
    if (!_nicknameTfd) {
        _nicknameTfd = [[UITextField alloc] init];
        _nicknameTfd.font = Font(16);
        _nicknameTfd.clearButtonMode = UITextFieldViewModeAlways;
        [_nicknameTfd becomeFirstResponder];
    }
    return _nicknameTfd;
}

- (UIButton *)clickBtn {
    if (!_clickBtn) {
        _clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _clickBtn.titleLabel.font = Font(19);
        _clickBtn.layer.cornerRadius = 5;
        _clickBtn.layer.backgroundColor = [RGB(47, 201, 105, 1) CGColor];
        _clickBtn.layer.masksToBounds = YES;
        [_clickBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_clickBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clickBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
